﻿

namespace Forest.Lion.Web.Controllers {
    using System;
    using System.Web.Mvc;
    using Forest.Lion.Domain.Contracts;
    using Forest.Lion.Web.Models;
    using Microsoft.Practices.ServiceLocation;
    using Forest.Lion.Domain.Services;
    public class MessageTemplateController : AuthorizedController {


        public MessageTemplateController(IUserServices userServices,
          IServiceLocator serviceLocator)
            : base(userServices, serviceLocator) {
        }

        public ActionResult List() {
            var items = Using<MessageTemplateAppService>().GetMessageTemplatesForCompany(CurrentUserId);

            return View(items);
        }

        public ActionResult Add() {
            var formModel = new MessageTemplateFormModel();

            formModel.Id = Guid.NewGuid();

            return View(formModel);
        }

        [HttpPost]
        public ActionResult Add(MessageTemplateFormModel formModel) {

            if (ModelState.IsValid) {
                Using<MessageTemplateAppService>().Create(formModel, CurrentUserId);
                return RedirectToAction("List");
            }

            return View(formModel);
        }

        public ActionResult Edit(Guid id) {
            var entity = Using<MessageTemplateAppService>().GetMessageTemplate(id);

            var formModel = new MessageTemplateFormModel {
                Id = entity.Id,
                Subject = entity.Subject,
                TemplateContent = entity.TemplateContent
            };

            return View(formModel);
        }

        [HttpPost]
        public ActionResult Edit(MessageTemplateFormModel formModel) {
            if (ModelState.IsValid) {
                Using<MessageTemplateAppService>().Update(formModel, CurrentUserId);
                return RedirectToAction("List");
            }
            return View(formModel);
        }

    }
}
